import { getToken, setToken } from '@/utils/auth.js'
import { sysLogin, sysProfile, sysUser } from '@/api/user'

const state = {
    token: getToken() || '',
    userInfo: ''
}
const mutations = {
    setLogin(state, value) {
        state.token = value // vuex
        setToken(value) // 浏览器
    },
    // 设置用户信息
    setUserInfo(state, value) {
        state.userInfo = value
    },
    // 退出要清空用户信息
    logout(state) {
        state.token = ''
        state.userInfo = ''
    }
}
const actions = {
    async toLogin({ commit }, value) {
        const res = await sysLogin(value)
        commit('setLogin', res.data)
            // console.log(res)
    },
    async getUserInfo(store) {
        const res = await sysProfile()
            // console.log(res)
        const res2 = await sysUser(res.data.userId)
        console.log(res2)
        store.commit('setUserInfo', Object.assign({}, res.data, res2.data))
    }
}
const getters = {}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}